package com.mflex.shelflocationquery.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mflex.shelflocationquery.R;
import com.mflex.shelflocationquery.model.Users;
import com.mflex.shelflocationquery.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.app.AppUtils;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

public class LoginActivity extends BaseActivity {

    private TextView txtTtitle;
    private LinearLayout loginLogin;

    private EditText txtUser;

    private EditText txtPwd;
    private Button btnLogin;

    private TextView txtVersion;
    private TextView txtApi;

    MiniLoadingDialog mMiniLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "登录中...");


        txtTtitle = (TextView) findViewById(R.id.txtTtitle);
        loginLogin = (LinearLayout) findViewById(R.id.loginLogin);

        txtUser = (EditText) findViewById(R.id.txtUser);

        txtPwd = (EditText) findViewById(R.id.txtPwd);
        btnLogin = (Button) findViewById(R.id.btnLogin);

        txtVersion = (TextView) findViewById(R.id.txtVersion);
        txtApi = (TextView) findViewById(R.id.txtApi);


        //隐藏配置项
        loginLogin.setVisibility(View.VISIBLE);


        //设置焦点
        txtUser.setFocusable(true);
        txtUser.setFocusableInTouchMode(true);
        txtUser.requestFocus();

        txtTtitle.setText(AppUtils.getAppName());
        txtVersion.setText("程序版本" + AppUtils.getAppVersionName());
//        txtApi.setText(BuildConfig.API_HOST);


        //设置监听
        setListener();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void setListener() {
        //登录按钮监听
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(txtUser.getText())) {
                    XToastUtils.error("MES账号为空!", 2000);
                    return;
                }
                if (StringUtils.isEmpty(txtPwd.getText())) {
                    XToastUtils.error("MES密码为空!", 2000);
                    return;
                }
                request(v);

            }
        });


    }


    /**
     * 登录请求
     */
    private void request(View v) {
        mMiniLoadingDialog.show();
        Users users= new Users();
        users.setUserNmae(txtUser.getText().toString());
        users.setPwd(txtPwd.getText().toString());
        XHttp.post("api/Login/checkLogin")
                .upJson(JsonUtil.toJson(users))
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<Object>() {
                    @Override
                    public void onSuccess(Object response) {
                        mMiniLoadingDialog.dismiss();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra("userName", txtUser.getText().toString());
                        intent.putExtra("userPassword", txtPwd.getText().toString());
                        startActivity(intent);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();

//                        XToastUtils.error(e.getMessage(),3000);
                        new MaterialDialog.Builder(v.getContext())
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }
}
